//
//  CollectionActivityTableViewController.m
//  LQH-Douban
//
//  Created by lanou3g on 15/10/26.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "CollectionActivityTableViewController.h"
#import "ActivityTableViewCell.h"
#import "CollectionDataHandle.h"
#import "ActivityTableViewCell.h"
#import "Message.h"
#import "UIImageView+WebCache.h"
#import "ActViewController.h"


@interface CollectionActivityTableViewController ()

@end

@implementation CollectionActivityTableViewController

- (void)dealloc
{
    [[CollectionDataHandle sharedCollection] closeDB];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [[CollectionDataHandle sharedCollection] openDB];
    
    self.navigationItem.title = @"我的活动";
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemEdit) target:self action:@selector(editAction:)];
    
    [self.tableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"cell_activity"];
    
    NSLog(@"%@", [CollectionDataHandle sharedCollection].activityArray);
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    
    return [CollectionDataHandle sharedCollection].activityArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_activity" forIndexPath:indexPath];
    
    // Configure the cell...
    Message *m = [CollectionDataHandle sharedCollection].activityArray[indexPath.row];
    
    cell.titleLabel.text = m.title;
    
    NSRange x ={5,11};
    NSString * s1 = [m.begin_time substringWithRange:x];
    NSString * s2 = [m.end_time substringWithRange:x];
    
    cell.beginEndTimeLabel.text = [NSString stringWithFormat:@"%@ -- %@", s1,s2];
    
    cell.AddressLabel.text = m.address;
    cell.CategoryNameLabel.text = [NSString stringWithFormat:@"类型 : %@", m.category_name];
    cell.WisherCountLabel2.text = [NSString stringWithFormat:@"%@",m.wisher_count];
    cell.ParticipantCountLabel2.text = [NSString stringWithFormat:@"%@",m.participant_count];
    
    cell.imv.image = m.activityImg;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActViewController *vc = [[ActViewController alloc] init];
    vc.message = [CollectionDataHandle sharedCollection].activityArray[indexPath.row];
    
    [self.navigationController showViewController:vc sender:nil];
    
}


// 删除cell

- (void)editAction:(UINavigationItem *)sender
{
    [self.tableView setEditing:!self.tableView.editing animated:YES];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 删除数据

    Message *m = [CollectionDataHandle sharedCollection].activityArray[indexPath.row];
    
    [[CollectionDataHandle sharedCollection] deleteActWithId:m.a_id];
    
    [[CollectionDataHandle sharedCollection].activityArray removeObjectAtIndex:indexPath.row];
    
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}









-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 190;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



@end
